package com.mmg.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.mmg.spzx.manager.service.CategoryBrandService;
import com.mmg.spzx.model.dto.product.CategoryBrandDto;
import com.mmg.spzx.model.entity.product.Brand;
import com.mmg.spzx.model.entity.product.CategoryBrand;
import com.mmg.spzx.model.vo.common.Result;
import com.mmg.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "分类品牌管理")
@RestController
@RequestMapping("/admin/product/categoryBrand")
public class CategoryBrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Operation(summary = "根据分类id查询品牌")
    @GetMapping("/findBrandByCategoryId/{categoryId}")
    public Result findBrandByCategoryId(@PathVariable Long categoryId) {
        List<Brand> brandList = categoryBrandService.findBrandByCategoryId(categoryId);
        return Result.build(brandList, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除分类品牌")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        categoryBrandService.delete(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改分类品牌")
    @PutMapping("update")
    public Result update(@RequestBody CategoryBrand categoryBrand) {
        categoryBrandService.update(categoryBrand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增分类品牌")
    @PostMapping("/save")
    public Result save(@RequestBody CategoryBrand categoryBrand) {
        categoryBrandService.save(categoryBrand);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findByPage(@PathVariable("page") Integer page,
                             @PathVariable("limit") Integer limit,
                             CategoryBrandDto categoryBrandDto) {
        PageInfo<CategoryBrand> categoryBrandPageInfo = categoryBrandService.findByPage(page, limit, categoryBrandDto);
        return Result.build(categoryBrandPageInfo, ResultCodeEnum.SUCCESS);
    }
}
